package com.yyplatform.frontend.platform.account.controller;

import static com.yyplatform.frontend.Constants.URL_PREFIX;

import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yyplatform.frontend.platform.account.dto.UserInfoDTO;
import com.yyplatform.frontend.infra.exception.ErrorCode;
import com.yyplatform.frontend.infra.exception.ServiceException;
import com.yyplatform.frontend.infra.model.RestResponse;
import com.yyplatform.frontend.infra.web.annotation.LoginRequired;
import com.yyplatform.frontend.infra.web.jwt.CurrentUserHolder;

import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-10-18
 */
@CrossOrigin
@RequestMapping(URL_PREFIX + "/user/")
@RestController
@Slf4j
public class UserController {

    private ModelMapper modelMapper;

    @LoginRequired
    @GetMapping("info")
    public RestResponse<UserInfoDTO> info(CurrentUserHolder currentUserHolder) {
        if (currentUserHolder.getUserId() == 0) {
            throw ServiceException.of(ErrorCode.UNAUTHORIZED);
        }

        return RestResponse.ok(modelMapper.map(currentUserHolder.getCurrentUser(), UserInfoDTO.class));
    }


}
